import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_book/data/enum/loading_status.dart';
import 'package:flutter_book/data/model/book_chapter_model.dart';
import 'package:flutter_book/data/model/book_model.dart';
import 'package:flutter_book/data/temp_data.dart';
import 'package:flutter_book/page/book/controller/book_page_controller.dart';
import 'package:get/get.dart';
import 'animation_util/data/animation_type.dart';
import 'dart:ui' as ui;

import 'animation_util/book_animation_page.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';

class BookReadPage extends StatefulWidget {
  final BookModel bookModel;

  BookReadPage(this.bookModel);

  @override
  _BookReadPageState createState() => _BookReadPageState();
}

List<ui.Image> listImage = [];

class _BookReadPageState extends State<BookReadPage> with SingleTickerProviderStateMixin {
  GlobalKey canvasKey = GlobalKey();

  late BookAnimationPageController _controller;
  final BookPageController _bookPageController = BookPageController();

  @override
  void initState() {
    super.initState();
    _controller = BookAnimationPageController(this, canvasKey)
      ..setAnimationType(AnimationType.TYPE_ANIMATION_SIMULATION_TURN);
    initData();
    initController();
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark, //黑色是dark 白色是light
      child: Scaffold(
        appBar: null,
        body: GetBuilder<BookPageController>(
          init: _bookPageController,
          initState: (_) {
            _bookPageController.init(widget.bookModel.id ?? "", initCompleteListener: () {
              _controller.setBookContentController(_bookPageController, false);
            });
          },
          dispose: (_) {
            _bookPageController.currentChapterIndex = 0;
            _bookPageController.chapterModelList.clear();
          },
          builder: (ctr) {
            return body();
          },
        ),
      ),
    );
  }

  Widget body() {
    return GetBuilder<BookPageController>(
        id: BookPageController.currentString,
        builder: (ctl) {
          if (ctl.currentStatus == LoadingStatus.STATUS_COMPLETED) {
            return BookAnimationPage(controller: _controller);
          } else if (ctl.currentStatus == LoadingStatus.STATUS_LOADING) {
            return _getLoadingUi();
          } else {
            "数据加载错误:${ctl.currentStatus}".print;
            return _getErrorUi();
          }
        });
  }

  Widget _getLoadingUi() {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            '加载中...',
            style: TextStyle(fontSize: 16.0),
          ),
          CircularProgressIndicator(
            strokeWidth: 1.0,
          )
        ],
      ),
    );
  }

  Widget _getErrorUi() {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            '数据加载错误',
            style: TextStyle(fontSize: 16.0),
          ),
        ],
      ),
    );
  }

  initController() {
    _controller.onTapListener(() {
      "点击了界面".print;
    });
    _controller.onLongTapListener(() {
      "长按了界面".print;
    });
  }

  //初始化测试图片的数据
  initData() async {
    // listImage.addAll(await TempData.getImageList());
    // _controller.setPictureList(listImage, false);
  }
}
